const {
    Mysql
} = require('../tools/utils');
const {
    checkOrder
} = require('../tools/checkOrder')
module.exports = async ctx => {
    let {
        decodeStr
    } = ctx.request.body;
    decodeStr = decodeStr.replace(/\&/g, "A").replace(/\%/g, "B");
    decodeStr = Buffer.from(decodeStr, 'base64').toString('utf-8');
    let userInfo = JSON.parse(decodeStr);
    let {
        uid,
        vid,
        orderId,
        openId,
        payAmount,
    } = userInfo;
    try {
        let checkRes = await checkOrder(orderId);
        if (checkRes.trade_state == 'SUCCESS') {
            let sql1 = `INSERT INTO orderList(uid,orderId,money,type) VALUES(${uid},'${orderId}',${payAmount},'VIDEO')`;
            let sql2 = `INSERT INTO buyVideo(uid,vid,orderId,money) VALUES(${Number(uid)},${Number(vid)},'${orderId}',${Number(payAmount)})`;
            try {
                await Mysql(sql1);
                let res = await Mysql(sql2);
                if (res) {
                    ctx.state.data = {
                        code: '001'
                    }
                }
            } catch (error) {
                ctx.state.data = {
                    code: '002'
                }

            }
        }
    } catch (error) {
        ctx.state.data = {
            code: '002'
        }
    }

}